<?php
namespace app\admin\controller;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Vod\Vod;
use app\common\entity\BackResult;
use app\common\service\UploadService;
use app\Request;
use think\App;
use app\common\model\VideoList as VideoListModel;

/**
 * 阿里云视频管理
 * Class Video
 * @package app\admin\controller
 */
class Video extends Base
{
    private $regionId = 'cn-shanghai';

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->initVodClient();
    }

    private function initVodClient(){
        $accessKeyId = env('aliyun.ali_access_key_id');
        $accessKeySecret = env('aliyun.ali_access_key_secret');
        AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
            ->regionId($this->regionId)
            ->connectTimeout(1)
            ->timeout(3)
            ->asDefaultClient();
    }

    public function createUploadVideo(Request $request){
        $data = [
            'title'=>$request->param("title"),
            'fileName'=>$request->param("fileName"),
        ];
        $vodRequest = Vod::v20170321()->createUploadVideo();
        $vodRequest->withTitle($data['title']);//标题，UTF8,128大小
        $vodRequest->withFileName($data['fileName']);//视频源文件名
        $result = $vodRequest->request();
        return BackResult::success(['UploadAddress'=>$result->UploadAddress,'UploadAuth'=>$result->UploadAuth,"videoId"=>$result->VideoId]);
    }

    public function refreshUploadInfo(Request $request){
        $videoId = $request->param("videoId");
        $result = Vod::v20170321()
            ->refreshUploadVideo()
            ->withVideoId($videoId)
            ->request();

        return BackResult::success(['UploadAddress'=>$result->UploadAddress,'UploadAuth'=>$result->UploadAuth,"videoId"=>$result->VideoId]);
    }

    public function deleteVideo(Request $request){
        $videoId = $request->param("videoId");

        //检查视频是否被使用 如果已经使用则不能删除
        $count = VideoListModel::where(['out_video_id'=>$videoId])->count();
        if($count){
            return BackResult::error([],"该视频在使用中，不能删除");
        }
        try{
            Vod::v20170321()
                ->deleteVideo()
                ->withVideoIds($videoId)
                ->connectTimeout(60)
                ->timeout(65)
                ->request();
            return BackResult::success([],"删除请求发送成功 视频随后将被删除");
        }catch (\Exception $e){
            return BackResult::error([],"操作失败");
        }
    }

    public function getPlayInfo(Request $request){
        $videoId = $request->param("videoId");
        $result = Vod::v20170321()->getPlayInfo()->withVideoId($videoId)
            ->format("JSON")
            ->request();

        return BackResult::success([
            'VideoBase'=>$result['VideoBase'],
            'PlayInfoList'=>$result['PlayInfoList'],
        ]);
    }

    public function getVideoList(Request $request){
        $page = $request->param("page",1);
        $pageSize = $request->param('pageSize',10);

        $result = Vod::v20170321()
            ->getVideoList()
            ->withPageNo($page)
            ->withPageSize($pageSize)
            ->connectTimeout(60)
            ->timeout(65)
            ->request();

        return BackResult::success(['videoList'=>$result['VideoList'],'total'=>$result['Total']]);
    }

}